package com.manage.service;

import java.util.List;

import org.apache.log4j.Logger;

import com.manage.config.Config;
import com.manage.model.CpInfo;
import com.service.AbstractService;
import com.util.ConfigUtil;
import com.utils.dbcp.ConnectionManager;
import com.utils.dbcp.ModelGenerator;

public class CpInfoServiceImpl  extends AbstractService implements CpInfoService {

	private Logger logger;
	public CpInfoServiceImpl()
	{
		logger = Logger.getLogger(this.getClass());
		this.dbcp = ConfigUtil.get(Config.BOSSSECTION, Config.KEY_DBCP_NAME);
	}

	@Override
	public CpInfo getCpInfoById(int id) {
		String sql="select * from t_cpinfo where id="+id;
		ModelGenerator<CpInfo> dbcpmg = new ModelGenerator<CpInfo>(CpInfo.class);
		logger.debug("[method:getCpItnfoById] [sql:"+sql+"]");
		return dbcpmg.generate(dbcp, sql);
	}

	@Override
	public List<CpInfo> getCpInfos() {
		String sql="select * from t_cpinfo order by createdate desc";
		ModelGenerator<CpInfo> dbcpmg = new ModelGenerator<CpInfo>(CpInfo.class);
		logger.debug("[method:getCpInfos] [sql:"+sql+"]");
		return dbcpmg.generateList(dbcp, sql);
	}

	@Override
	public boolean saveCpInfo(CpInfo cpInfo,String type) {
		String sql ="";
		if(type.equalsIgnoreCase("insert"))
		{
			cpInfo.setId(getTableId("t_cpinfo"));
			sql = "insert into t_cpinfo(id,cpname,shortname,cpremark,bd,createuser,createdate) values (" +
					""+cpInfo.getId()+",'"+cpInfo.getCpName()+
					"','"+cpInfo.getShortName()+
					"','"+cpInfo.getCpRemark()+"','"+cpInfo.getBd()+"','"+cpInfo.getCreateUser()+"','"+cpInfo.getCreateDate()+"')";
		}else if (type.equalsIgnoreCase("update"))
		{
			sql="update t_cpinfo set cpname='"+cpInfo.getCpName()+
			"',shortname='"+cpInfo.getShortName()+"',bd='"+cpInfo.getBd()+"',cpremark='"+cpInfo.getCpRemark()+"' where " +
					"id ="+cpInfo.getId();
		}
		else if (type.equalsIgnoreCase("delete"))
		{
			sql="delete from t_cpinfo where " +
			"id ="+cpInfo.getId();
		}
		logger.debug("[method:saveCpInfo] [sql:"+sql+"]");
		return ConnectionManager.getInstance().execute(dbcp, sql);
	}


}
